/*
 * Copyright (C) 2006 The Android Open Source Project
 * Copyright (C) 2020 Agrui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <mindroid/os/Registrant.h>

namespace mindroid {

Registrant::Registrant(Handler& h, int what, void* obj) {
    refH = &h;
    this->what = what;
    userObj = obj;
}

Registrant::~Registrant() {
    userObj = NULL;
}


void Registrant::notifyRegistrant() {
    internalNotifyRegistrant (NULL, 0);
}

void Registrant::notifyResult(void* result) {
    internalNotifyRegistrant (result, 0);
}

void Registrant::notifyException(int exception) {
    internalNotifyRegistrant (NULL, exception);
}


void Registrant::notifyRegistrant(AsyncResult& ar) {
    internalNotifyRegistrant (ar.result, ar.errorCode);
}

void Registrant::internalNotifyRegistrant (void* result, int errorCode) {
    Handler* h = getHandler();
    Message* msg = Message::obtain();
    msg->what = what;
    msg->obj = new AsyncResult(userObj, result, errorCode);
    h->sendMessage(*msg);
}


Message* Registrant::messageForRegistrant() {
    Handler* h = getHandler();
    Message* msg = h->obtainMessage();
    msg->what = what;
    msg->obj = userObj;
    return msg;
}


}
